package edu.hcp.longshorturl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hcp.longshorturl.entity.CustomEntity;
import edu.hcp.longshorturl.mapper.CustomMapper;
import edu.hcp.longshorturl.service.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**                                                                                ____________________
      _                _                                                           < 神兽护体，永无bug! >
    | |__  _   _  ___| |__   ___ _ __   __ _ _ __   ___ _ __   __ _                --------------------
   | '_ \| | | |/ __| '_ \ / _ \ '_ \ / _` | '_ \ / _ \ '_ \ / _` |                       \   ^__^
  | | | | |_| | (__| | | |  __/ | | | (_| | |_) |  __/ | | | (_| |                        \  (oo)\_______
 |_| |_|\__,_|\___|_| |_|\___|_| |_|\__, | .__/ \___|_| |_|\__, |                           (__)\       )\/\
                                   |___/|_|                |___/                                ||----w |
                                                                                                ||     ||
 * @author huchengpeng
 * @date 2021/8/21 19:26
 * @version 
 * @Description 
 */
@Service("customService")
public class CustomServiceImpl extends ServiceImpl<CustomMapper, CustomEntity> implements ICustomService {

    @Autowired
    private CustomMapper customMapper;

    @Override
    public CustomEntity queryByShortUrl(String shortUrl) {
        LambdaQueryWrapper<CustomEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomEntity::getCustomUrl,shortUrl);
        final List<CustomEntity> customEntities = baseMapper.selectList(queryWrapper);
        if(customEntities.size() >= 1){
            return customEntities.get(0);
        }
        return null;
    }

    @Override
    public boolean insertCustomUrl(CustomEntity customEntity) {
        Integer rows = customMapper.insertCustomUrl(customEntity);
        if(rows < 1){
            return false;
        }
        return true;
    }
}